<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>联系信息</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css" />

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- city -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/city-picker.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/ContactAdd.less" />
</head>

<body>
    <!-- 信息录入 -->
    <div class="info">
        <h3 class="title wow fadeInUp">添加通讯录联系人</h3>

        <p class="desc wow fadeInUp" data-wow-delay="200ms">
            您可在本页录入您的联系人信息，录入成功后名片将会出现在首页。
        </p>
    </div>

    <form class="contact wow fadeInUp" data-wow-delay="300ms" id="contact">
        <div class="form-group">
            <label for="nickname">昵称</label>
            <input type="text" class="form-control" id="nickname" name="nickname" placeholder="请输入昵称" />
        </div>

        <div class="form-group">
            <label for="phone">手机</label>
            <input type="text" class="form-control" id="mobile" name="mobile" placeholder="请输入手机号" />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select id="gender" class="form-control">
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>


        <div class="form-group">
            <label for="typeid">分类</label>
            <select class="form-control" id="typeid">  
                <option value='0'>请选择类型</option>
            </select>
        </div>

        <div class="form-group" style="position: relative;">
            <label for="region">地区</label>
            <input type="text" class="form-control" readonly data-toggle="city-picker" name="region" id="region" placeholder="请选择地区" />
        </div>

        <div class="form-group">
            <label for="remark">备注</label>
            <input type="text" class="form-control" id="remark" name="remark" placeholder="请输入备注" />
        </div>

        <div class="action">
            <button class="btn">提交</button>
        </div>
    </form>
</body>

</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap-validate -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- city -->
<script src="./assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/city-picker.min.js"></script>

<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 服务请求 -->
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- helpers -->
<script src="./assets/plugins/helpers.js"></script>


<script>
    //实例化wow.js
    new WOW().init();

    // 定义存放登录信息的变量
    let business = {};

    (async () => {
        let result = await check(true, true);

        // 获取验证成功后的数据并且赋值给变量
        business = result.data ? result.data : {};

        getTypesOpetions();

    })();

    //获取通讯录分类下拉信息
    async  function getTypesOpetions(){
         let res = await POST({
            url: 'card/card/type',
        });

        // let typeList = res.data;
        if(res.code === 1){

            if (res.data.length > 0) {

                let temp = '';
                for (let item of res.data) {
                    // $('#typeid').append(`<option value=${val.id}>${val.name}</option>`);
                    temp += `<option value=${item.id}>${item.name}</option>`;

                }
                $('#typeid').html(temp);
            }    
        }  
    }


    // 实例化表单验证器
    $('#contact').bootstrapValidator({
        // 填写配置项
        message: '表单内容不能为空',// 所有字段默认错误信息
        // 字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            nickname: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '姓名不能为空'
                    }
                }
            },
            mobile: {
                validators: {
                    // 必填
                    notEmpty: {
                        message: '手机号不能为空'
                    },
                    // 校验字段长度
                    stringLength: {
                        min: 11,// 最小长度
                        max: 11,// 最大长度
                        message: '手机号必须是11位'
                    },
                    // 正则校验
                    regexp: {
                        regexp: /^1[3-9]\d{9}$/,
                        message: '手机号格式错误'
                    }
                }
            }, 
        }
    }).on('success.form.bv', function (e) {
        // 阻止多次提交
        e.preventDefault();
    });

    $('#contact').on('submit', async function () {
        // 判断表单验证是否失败
        if (!$('#contact').data('bootstrapValidator').isValid()) {
            // 验证没有通过就不执行后面代码
            return false;
        }
        // 获取相应数据并且组装
        let params = {
            businessid: business.id,
            nickname: $.trim($('#nickname').val()),
            mobile: $.trim($('#mobile').val()),
            gender: $.trim($('#gender').val()),
            typeid: $.trim($('#typeid').val()),
            remark: $.trim($('#remark').val())
        }

        // 地区处理
        let citypicker = $('#region').data("citypicker");
        let region = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");

        if (region) {
            params.region = region;
        }

        console.log('params:', params);
        // 发起请求
        let result = await POST({
            url: 'card/card/add',
            params
        });


        if (result.code === 1) {

            mui.toast(result.msg, { duration: 1500 });

            return false;
        }
        else {
            mui.toast(result.msg, { duration: 1500 });

            return false;
        }

    })

</script>